<template>
  <el-dialog :visible="showDialog" :title="showTitle" @close="btnCancel">
    <el-form
      ref="formData"
      label-width="120px"
      :rules="rules"
      :model="formData"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width: 80%" />
      </el-form-item>
      <el-form-item label="部门代码" style="width: 80%" prop="code">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager">
          <el-option
            v-for="item in people"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门描述" style="width: 80%" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="4">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button size="small" type="primary" @click="btnOk">确认</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import {
  getDepartments,
  addDepartments,
  getDepartmentsDetail,
  updateDepartments
} from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object
    }
  },
  data() {
    // 自定义校验规则
    const checkNameRepeat = async (rule, value, callback) => {
      const { depts } = await getDepartments()
      var isRepeat
      if (this.formData.id) {
        isRepeat = depts
          .filter(
            (item) =>
              item.pid === this.formData.pid && item.id !== this.formData.id
          )
          .some((item) => item.name === value)
      } else {
        isRepeat = depts
          .filter((item) => item.pid === this.treeNode.id)
          .some((item) => item.name === value)
      }

      isRepeat ? callback(new Error('部门下已经有相同的名字了')) : callback()
    }
    const checkCodeRepeat = async (rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat
      if (this.formData.id) {
        isRepeat = depts.some(
          (item) => item.id !== this.formData.id && item.code === value && value
        )
      } else {
        isRepeat = depts.some((item) => item.code === value && value)
      }
      isRepeat
        ? callback(new Error(`部门里已经有相同的${value}了`))
        : callback()
    }
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      people: [],
      rules: {
        name: [
          { required: true, message: '名字不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '字符长度为1-50', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: checkNameRepeat
          }
        ],
        code: [
          { required: true, message: '编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '字符长度为1-50', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: checkCodeRepeat
          }
        ],
        manager: [
          { required: true, message: '负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '字符长度为1-300', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getEmployeeSimple()
  },
  methods: {
    // 获取部门负责人
    async getEmployeeSimple() {
      this.people = await getEmployeeSimple()
    },
    // 确认提交
    btnOk() {
      this.$refs.formData.validate(async (isOk) => {
        if (isOk) {
          //  验证通过可以提交
          // await addDepartments({ ...this.formData, pid: this.treeNode.id })
          // // 调取成功之后告诉父组件重新拉去数据
          // this.$emit('addDepts')
          // this.$emit('update:showDialog', false)
          // 首先要判断是编辑还是新增
          if (this.formData.id) {
            await updateDepartments(this.formData)
          } else {
            await addDepartments({ ...this.formData, pid: this.treeNode.id }) // 调用新增接口 添加父部门的id
          }
          this.$emit('addDepts')
          this.$emit('update:showDialog', false)
        }
      })
    },

    // 取消和关闭对话框
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.formData.resetFields()
      this.$emit('update:showDialog', false)
    },

    // 获取部门详情
    async getDepartmentsDetail(id) {
      this.formData = await getDepartmentsDetail(id)
    }
  }
}
</script>
<style scoped></style>
